import Request from "@/sdk/pocky-request/index.js";
import COMM from "@/common/common.js";
/**
 * APP更新
 */
const upDate = {
	// 状态
	state: {
		updateInfo: {
			notice: '',
			type: 1
		},
		nowAppInfo: {
			versionCode: 100,
			version: '1.0.0'
		}

	},
	// 派生
	getters: {
		getUpdateInfo: state => {
			if (state.updateInfo.notice) {
				return state.updateInfo.notice.split('/n')
			} else {
				return '';
			}
		}
	},
	// 同步方法 commit
	mutations: {
		updateInfo: (state, payload) => {
			state.updateInfo = payload
		},
		nowAppInfo: (state, payload) => {
			state.nowAppInfo = payload
		},
	},
	// 异步方法 dispatch
	actions: {
		/**
		 * @description 从接口获取更新信息
		 * @param {Object} context
		 */
		apiGetUpdateInfo(context) {
			return new Promise((resolve, reject) => {
				// TODO 生产
				let _data = {
					channel: uni.getSystemInfoSync().platform == 'ios' ? '10000' : plus.runtime.channel,
					platform: uni.getSystemInfoSync().platform.toLocaleUpperCase()
				}

				Request().request({
					url: '/common/check_version.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						console.log(res);

						this.commit("updateInfo", res.data.data)

						resolve()
					} else {
						reject()
					}
				}).catch(err => {
					uni.showToast({
						title: '当前网络不可用，请检查网络',
						icon: 'none'
					});
				})
			})

		},
		/**
		 * @description  获取当前APP信息   版本与版本code
		 * @param {Object} context
		 */
		getNowAppInfo(context) {
			// #ifdef APP-PLUS
			plus.runtime.getProperty(plus.runtime.appid, res => {
				let _vCode = parseInt(res.version.split('.').join(''));
				let _now = {
					version: res.version,
					versionCode: _vCode
				}
				this.commit("nowAppInfo", _now)
				console.log(_now);
			})
			// #endif
			// #ifdef H5
			let _now = {
				version: '1.0.0',
				versionCode: 100
			}
			this.commit("nowAppInfo", _now);
			// #endif


			// let _now={
			// 		version:'1.0.0',
			// 		versionCode:100
			// }
			// if(uni.getStorageSync("cacheVersion")){
			// 	_now=uni.getStorageSync("cacheVersion")
			// }
			// this.commit("nowAppInfo",_now)


		},
		/**
		 * @description 检查安装更新
		 * @param {Object} context
		 */
		checkUpDate(context) {
			return new Promise(resolve => {
				if (context.state.updateInfo) { //接口有更新信息
					let _localVersion = context.state.nowAppInfo.version.split('.'); //当前APP
					let _newVersion = context.state.updateInfo.latest.split('.'); //更新

					if (parseInt(_localVersion[0]) != parseInt(_newVersion[0])) { //首位不同
						let _up = context.state.updateInfo;
						_up.type = 2;
						this.commit("updateInfo", _up)
						uni.$emit("showForceUpdata")
					} else {
						if (parseInt(_localVersion[1]) < parseInt(_newVersion[1])) { //当前中位小于更新中位
							let _up = context.state.updateInfo;
							_up.type = 2;
							this.commit("updateInfo", _up)
							uni.$emit("showForceUpdata")
						} else if (parseInt(_localVersion[1]) == parseInt(_newVersion[1])) { //当前中位等于更新中位

							if (parseInt(_localVersion[2]) < parseInt(_newVersion[2])) { //当前末位小于更新末位
								let _up = context.state.updateInfo;
								_up.type = 1;
								this.commit("updateInfo", _up)
								if (uni.getStorageSync("localVersion")) { //有缓存信息
									let _catchVersion;
									if (typeof uni.getStorageSync("localVersion") == 'number') {
										_catchVersion = uni.getStorageSync("localVersion").split(''); //缓存版本
									} else {
										_catchVersion = uni.getStorageSync("localVersion").split('.'); //缓存版本
									}
									if (parseInt(_catchVersion[2]) < parseInt(_newVersion[2])) {
										COMM.updateApp(context.state.updateInfo)
									} else {
										resolve()
									}
								} else { //无缓存信息
									COMM.updateApp(context.state.updateInfo)
								}


							} else {
								resolve()
							}


						} else {
							resolve()
						}
					}
				} else {
					// 接口没有更新信息
					resolve()
				}
			})

		},


	}
}

export default upDate
